package com.vf.admin.client.controller;

import com.vf.admin.client.common.ClientBaseController;
import com.vf.admin.client.common.ClientConstants;
import com.vf.admin.client.common.RedisKeys;
import com.vf.admin.client.service.AppRoomAdminService;
import com.vf.admin.client.service.AppSystemService;
import com.vf.admin.client.service.AppUserService;
import com.vf.admin.common.entity.Result;
import com.vf.admin.common.redis.RedisUtil;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppMessageRecord;
import com.vf.admin.miaodong.entity.AppUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * @author anxiang
 * @version 1.0.0
 * @ClassName AppXiTongXiaoXiController.java
 * @Description 系统消息接口
 * @createTime 2021年07月22日 17:52:00
 */
@Controller
@RequestMapping("/appsystem")
public class AppXiTongXiaoXiController extends ClientBaseController {


    @Autowired
    AppSystemService appSystemService;
    @Autowired
    AppUserService appUserService;
    @Autowired
    AppRoomAdminService appRoomAdminService;
    @Autowired
    RedisUtil redisUtil;


    /**
     * 获取系统消息
     * @param pageno
     * @return
     */
    @ResponseBody
    @RequestMapping("/xiaoxi")
    public Result xiaoxi(Integer pageno) {
        try {
            if (pageno == null) {
                return this.errorJSON("参数错误");
            }
            Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
            //获取当前登录用户的user对象
            AppUser user = appUserService.get(userId);
            SplitPageBean splitPageBean = new SplitPageBean(pageno, 10);
            List<AppMessageRecord> appMessageRecordList = appSystemService.findList(user.getId(),splitPageBean);
            if (appMessageRecordList == null || appMessageRecordList.size() == 0) {
                return this.sucessPage(new ArrayList<>(), splitPageBean.getNextPageNo());
            } else {
                return this.sucessPage(appMessageRecordList, splitPageBean.getNextPageNo());
            }
        } catch (Exception e) {
            e.printStackTrace();
            return this.errorJSON("网络繁忙，请稍后重试");
        }
    }


    /**
     * 根据ID删除个人的系统通知信息
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/deleteNotice")
    public Result deleteNotice(@RequestParam(name = "id") Integer id){
        Integer userId = (Integer) redisUtil.get(RedisKeys.userToken + request.getHeader(ClientConstants.client_toke));
        AppMessageRecord record = appSystemService.get(id);
        if(record.getLeixing().equals(2) && record.getUserid().equals(userId)){
            appSystemService.del(id);
            return sucessJSON("成功");
        }else{
            return errorJSON("不可删除");
        }
    }


}
